/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import java.io.File;
import java.io.IOException;
import javax.swing.filechooser.FileSystemView;

/**
 * Override of the FileSystemView because i want to only allow users to select
 * files from a single folder that i selected
 *
 * @author Vulstar
 */
public class FileRestrictionSelect extends FileSystemView {

    private final File[] rootDirectories;

    /**
     * If a different directory is open it will return to the default one
     *
     * @author Koen Ursem
     * @param rootDirectory the directory given at the settingsdialog.java
     */
    public FileRestrictionSelect(File rootDirectory) {
	this.rootDirectories = new File[]{rootDirectory};
    }

    /**
     * If a different directory is open it will return to the default one
     *
     * @param rootDirectories the directoryies given at the settingsdialog.java
     * @author Koen Ursem
     */
    public FileRestrictionSelect(File[] rootDirectories) {
	this.rootDirectories = rootDirectories;
    }

    /**
     * Disable creating new folders so users can cheat on the directory
     *
     * @param containingDir the commando interception for creating a new folder
     * @author Koen Ursem
     */
    @Override
    public File createNewFolder(File containingDir) throws IOException {
	throw new UnsupportedOperationException("Unable to create directory");
    }

    /**
     * Redirects the filechooser to the root directory that is inserted on
     * creation
     *
     * @author Koen Ursem param File[] returns the rootDirectories for checking
     */
    @Override
    public File[] getRoots() {
	return rootDirectories;
    }

    /**
     * Changes the home button to go to the program directory instead of the
     * users default docs
     *
     * @author Koen Ursem @params getHomeDirectory intercepts the user pressing
     * the home button so we change the return
     */
    // nasty cheat user.dir relocates to the directroy folder, wanted to use rootDirectories but it didnt work that way with the given return, Koen
    @Override
    public File getHomeDirectory() {
	return createFileObject(System.getProperty("user.dir"));
    }

    /**
     * Makes sure that the selected file was realy in the root directroy
     * (failsave) if it goes wrong
     *
     * @param file is the selected file to check in its root folder
     * @author Koen Ursem
     */
    @Override
    public boolean isRoot(File file) {
	for (File root : rootDirectories) {
	    if (root.equals(file)) {
		return true;
	    }
	}
	return false;
    }
}